import regeneratorRuntime from 'resource/libs/runtime'
import { websocket_origin } from 'common/config'
import { asyncCalculateDistance } from 'common/utils'

App({
  onShow () {
    this.global_data.app_show = true
    if (this.global_data.socket_closed) {
      this.alert('wss 已关闭')
      wx.connectSocket({
        url: websocket_origin
      })
    }
  },
  onHide () {
    this.global_data.app_show = false
  },
  global_data: {
    app_show: true, // 小程序是否在前台
    socket_closed: false, // websocket 是否已关闭

    token: '',
    phone: '', // 标识用户是否已绑定手机号码

    origin: '', // 出发地的地址
    o_region: [], // 出发地的省市区
    o_location: { // 出发地的经纬度
      latitude: '',
      longitude: ''
    },
    destination: '', // 目的地
    d_region: [],
    d_location: {
      latitude: '',
      longitude: ''
    },

    interval: [], // 不同时间段的价格表
    start: [], // 不同里程的价格表
    wait: {}, // 等待时长的价格表

    origin_money: 0, // 起步价
    km: 0, // 出发地到目的地的驾车距离
    km_money: 0, // 超出的里程费
    time: 0, // 出发地到目的地的驾车时长
    time_money: 0, // 看似时长费，但应该是等待费，小程序端暂时不用计算，默认为0
  },

  /**更新 global_data
   * @param {Object} options 包含需要更新的字段
   */
  updateGlobalData (options) {
    Object.assign(this.global_data, options)
  },

  // 计算 origin_money、km、km_money、time
  async calPrice () {
    let origin_money = 0
    let km = 0
    let km_money = 0
    let time = 0
    let {
      destination,
      d_location,
      o_location,
      interval,
      start
    } = this.global_data
    // 确保是正确的数据类型进行计算
    interval.forEach((item) => {
      item.price = Number(item.price)
    })
    start.forEach((item) => {
      item.distance = Number(item.distance)
      item.price = Number(item.price)
    })
    // 计算公里和耗时
    if (destination) {
      let d_res = await asyncCalculateDistance({
        mode: 'driving',
        from: o_location,
        to: [d_location]
      })
      if (d_res) {
        let { elements } = d_res.result
        km = elements[0].distance / 1000
        time = elements[0].duration / 60
      }
    }
    // 计算起步价
    if (interval.length) {
      let hour = new Date().getHours()
      let b_hour = Number(interval[0].time_begin.split(':')[0])
      let e_hour = Number(interval[1].time_begin.split(':')[0])
      if (hour >= b_hour && hour < e_hour) {
        origin_money = interval[0].price
      } else {
        origin_money = interval[1].price
      }
    } else if (start.length) {
      origin_money = start[0].price
    }
    // 计算超出的里程费
    if (start.length && km > start[0].distance) {
      let cal_k = Math.ceil(km)
      let cal_d = start[0].distance
      for (let i = 1; i < start.length; i++) {
        if (cal_k <= cal_d) {
          break
        } else if (i === start.length - 1) {
          km_money += Math.ceil((cal_k - cal_d) / start[i].distance) * start[i].price
        } else {
          km_money += start[i].price
          cal_d += start[i].distance
        }
      }
    }
    this.updateGlobalData({
      origin_money,
      km: km.toFixed(1),
      km_money,
      time: time.toFixed(0),
    })
  },

  alert (title) {
    // wx.showToast({
    //   title,
    //   icon: 'none'
    // })
  }
})